<!--
 * @Author: zhang00001 774004514@qq.com
 * @Date: 2024-06-17 10:18:04
 * @LastEditors: zhang00001 774004514@qq.com
 * @LastEditTime: 2024-07-26 16:14:07
 * @FilePath: \vue-admin\src\pages\active\ActivityForm.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <a-drawer
      :title="formData.notice_id ? '编辑' : '新增'"
      placement="right"
       :maskClosable='false'
    :keyboard='false'
      width="800"
      @close="showAddDialog = false"
      v-model:open="showAddDialog"
    >
      <a-form
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 20 }"
        ref="formRef"
        :rules="rules"
        :model="formData"
        label-position="top"
      >
        <a-form-item
          label="公告内容"
          required
          name="content"
        >
        
          <a-textarea
            v-model:value="formData.content"
            placeholder="请输入公告内容"
            :auto-size="{ minRows: 2, maxRows: 10 }"
          />
        </a-form-item>

        <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button @click="handleCancel">取消</a-button>
          <a-button
            style="margin-left: 20px"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
          >
            确认
          </a-button>
        </a-form-item>
      </a-form>
    </a-drawer>
  </div>
</template>

<script setup>
  import { noticeAdd, noticeUpdate } from '@/api/index.js'
  import { ref, defineEmits, defineExpose, defineProps, toRefs, nextTick } from 'vue'

  const emits = defineEmits(['activity-added', 'close'])
  const props = defineProps({
    showAddDialog: {
      type: Boolean,
      default: false
    }
  })
  const loading = ref(false)
  const formRef = ref()
  const rules = {
    content: [
      {
        required: true,
        message: '请输入公告内容',
        trigger: 'change'
      },
      {
        min: 2,
        max: 500,
        message: '长度2-500字',
        trigger: 'blur'
      }
    ]
  }
  const { showAddDialog } = toRefs(props)
  const formData = ref({
    content: ''
  })
  const uploadVisible = ref(false)
  const handleSubmit = () => {
    formRef.value.validate().then(() => {
      loading.value = true
      let http = formData.value.notice_id ? noticeUpdate : noticeAdd

      http(formData.value)
        .then((res) => {
          if (res.code == 200) {
            emits('success')
            formData.value = {
              content: ''
            }
          }
        })
        .catch((err) => {
          message.error(err.msg)
        })
        .finally(() => {
          loading.value = false
        })
    })
  }

  const handleCancel = () => {
    emits('close') // Close the dialog
  }
  const init = (row) => {
    nextTick(() => {
      formRef.value.resetFields()

      if (row) {
        let data = JSON.parse(JSON.stringify(row))
        formData.value = {
          content: data.content,
          notice_id: data.notice_id
        }
      } else {
        formData.value = {
          content: data.content
        }
      }
    })
    //
  }
  defineExpose({
    init
  })
</script>

<style scoped></style>
